package com.i2works.smartluolong.web.ocr;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.i2works.smartluolong.utils.common.HttpUtils;
import com.i2works.smartluolong.utils.common.ImageConverUtil;
/**
 * 网络图片文字识别
 * */
public class ocrTest5 {
	
	public static void main(String[] args) {
	    String host = "https://ocrapi-document.taobao.com";
	   String path ="/ocrservice/document";
	    String method = "POST";
	    String appcode = "0b987ff4178648c3be2c16f4d6a9b66b";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    //根据API的要求，定义相对应的Content-Type
	    headers.put("Content-Type", "application/json; charset=UTF-8");
	    Map<String, String> querys = new HashMap<String, String>();
	    //String bodys = "{//图像数据：base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边最大4096px，支持jpg/png/bmp格式，和url参数只能同时存在一个\"img\":\"\",//图像url地址：图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4M，最短边至少15px，最长边最大4096px，支持jpg/png/bmp格式，和img参数只能同时存在一个\"url\":\"\",//是否需要识别结果中每一行的置信度，默认不需要。true：需要false：不需要\"prob\":false}";

	    JSONObject  jsonObject=new 	 JSONObject();
	    String imageBase64 = ImageConverUtil.toBase64("D:\\2018830103743577.jpg");
	    jsonObject.put("img",imageBase64);
	    JSONObject  jsonConfigure=new 	 JSONObject();
	    jsonConfigure.put("prob", false);
	    String bodys=jsonObject.toJSONString();
	    System.out.println(bodys);
	    try {

	    HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	System.out.println(response.toString());
	    	//获取response的body
	    	System.out.println(EntityUtils.toString(response.getEntity()));
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	}

}
